import '../util/util.dart';

class PinsCell {
  Map<String, dynamic> user;
  String objectId;
  String uid;
  String content;
  List<String> pictures;
  int commentCount;
  int likedCount;
  String createdAt;
  Map<String, dynamic> topic;
  String url;
  String urlTitle;
  String urlPic;

  PinsCell(
      {this.commentCount,
        this.content,
        this.createdAt,
        this.likedCount,
        this.objectId,
        this.pictures,
        this.topic,
        this.uid,
        this.user,this.url,this.urlPic,this.urlTitle});

  factory PinsCell.fromJson(Map<String, dynamic> json) {
    Map<String, dynamic> user = new Map();
    user['avatarLarge'] = json['author_user_info']['avatar_large'];
    user['objectId'] = json['author_user_info']['user_id'];
    user['company'] = json['author_user_info']['company'];
    user['jobTitle'] = json['author_user_info']['job_title'];
    user['role'] = json['author_user_info']['role'];
    user['userName'] = json['author_user_info']['user_name'];
    user['currentUserFollowed'] = json['author_user_info']['currentUserFollowed'];

    Map<String, dynamic> topic = new Map();
    // 有的沸点没有topic
    if (json['topic'] != null) {
      topic['objectId'] = json['topic']['topic_id'];
      topic['title'] = json['topic']['title'];
    }

    List<String> pics = new List();
    // pics = json['pictures'];_TypeError (type 'List<dynamic>' is not a subtype of type 'List<String>')
    json['msg_Info']['pic_list'].forEach((ele) {
      pics.add(ele);
    });
    // 将时间戳转int类型，再把时间戳转换成DateTime类型
    var formatDate = DateTime.fromMillisecondsSinceEpoch(int.parse(json['msg_Info']['ctime']) * 1000);

    return PinsCell(
        commentCount: json['msg_Info']['comment_count'],
        content: json['msg_Info']['content'],
        createdAt: Util.getTimeDuration(formatDate.toString()),
        likedCount: json['msg_Info']['digg_count'],
        objectId: json['msg_id'],
        pictures: pics,
        topic: topic,
        uid: json['msg_Info']['user_id'],
        url: json['msg_Info']['url'],
        urlPic: json['msg_Info']['url_pic'],
        urlTitle: json['msg_Info']['url_title'],
        user: user);
  }
}
